package com.quasar.hpatient.view.prescription;

import android.view.View;
import android.widget.RelativeLayout;

import com.ada.adapter.RenderAdapter;
import com.quasar.hpatient.R;
import com.quasar.hpatient.base.BaseActivity;
import com.quasar.hpatient.constant.ConstantManger;
import com.quasar.hpatient.model.custommodel.AllprescriptionDataBean;
import com.quasar.hpatient.model.event.UpdateEvent;
import com.quasar.hpatient.model.medicalmodel.PrescriptionDayData;
import com.quasar.hpatient.onInterface.OnSelectPrescriptionListener;
import com.quasar.hpatient.presenter.SelectPrescriptionPresenter;
import com.quasar.hpatient.view.adapter.render.NowDrugRender;
import com.quasar.hpatient.widget.MyListView;
import com.vise.log.ViseLog;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.OptionsItem;
import org.androidannotations.annotations.OptionsMenu;
import org.androidannotations.annotations.ViewById;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

/**
 * 我的药单
 */
@OptionsMenu(R.menu.menu_recording)
@EActivity(R.layout.activity_at_prescription)
public class AtPrescriptionActivity extends BaseActivity implements OnSelectPrescriptionListener {

    private SelectPrescriptionPresenter selectPrescriptionPresenter;
    RenderAdapter<PrescriptionDayData.ListBean> adapter;

    List<PrescriptionDayData.ListBean> prescription = new ArrayList<>();
    private PrescriptionDayData prescriptionDayData;
    ArrayList<AllprescriptionDataBean> beanList = null;
    @ViewById(R.id.rv_list)
    MyListView rv_list;
    @ViewById(R.id.rl_prescription)
    RelativeLayout rl_prescription;

    //用于标识zhi8you当前药单可以跳转到编辑药品界面
    final String STATS = "AtPrescriptionActivity";

    @AfterViews
    void init() {
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
        initView();
        initData();
    }

    @Override
    protected void initView() {
        initToolbar();
        selectPrescriptionPresenter = new SelectPrescriptionPresenter(this);
        NowDrugRender nowDrugRender = new NowDrugRender(this);
        nowDrugRender.setStats(STATS);

        adapter = new RenderAdapter<>(this, nowDrugRender);


    }

    @Override
    protected void initData() {
        showDialog();
        selectPrescriptionPresenter.AtPrescription();
    }

    //调药记录
    @OptionsItem(R.id.action_recording)
    public void action_recording() {
        PharmacyRecordActivity_.intent(this).start();
    }

    //添加药品
    @OptionsItem(R.id.add_medicine)
    public void add_medicine() {

        String time = null;
        if (prescriptionDayData != null) {
            if (prescriptionDayData.getList() != null) {
                if (prescriptionDayData.getList().size()!= 0) {
                    time = prescriptionDayData.getList().get(0).getPrescriptionIndex();
                }
            }
        }

        if (time != null) {
            AddPrescriptionActivity_.intent(this).beanList(beanList).DataTime(time).start();
        } else {
            AddPrescriptionActivity_.intent(this).start();
        }

    }

    @Override
    public void onPrescriptionDaySuccess(PrescriptionDayData data) {
        prescription.clear();
        adapter.clear();

        if (data.getList() != null) {
            prescriptionDayData = data;
            prescription.addAll(data.getList());
        }
        adapter.addAll(prescription);
        rv_list.setAdapter(adapter);

        //映射数据到AllprescriptionDataBean
        int nums = data.getList().get(0).getPerscriptDetailList().size();
        if(nums==0){
            rl_prescription.setVisibility(View.VISIBLE);
        }else{
            rl_prescription.setVisibility(View.GONE);
        }
        beanList = new ArrayList<>();
        for (int i = 0; i < nums; i++) {
            AllprescriptionDataBean allprescriptionDataBean = new AllprescriptionDataBean();
            allprescriptionDataBean.setPer_name(data.getList().get(0).getPerscriptDetailList().get(i).getMName()); //填充名称
            allprescriptionDataBean.setDoseName(data.getList().get(0).getPerscriptDetailList().get(i).getDoseName()); //填充规格
            allprescriptionDataBean.setPackages(data.getList().get(0).getPerscriptDetailList().get(i).getPackages()); //包装
            allprescriptionDataBean.setStartTime(data.getList().get(0).getPerscriptDetailList().get(i).getPeriodBegin()); //时间
            allprescriptionDataBean.setTimeLen(data.getList().get(0).getPerscriptDetailList().get(i).getTimeLen()); // TimeLen
            allprescriptionDataBean.setUseTypeId(data.getList().get(0).getPerscriptDetailList().get(i).getUseTypeId());//如何服药
            allprescriptionDataBean.setUserPeriodId(data.getList().get(0).getPerscriptDetailList().get(i).getUserPeriodId() + ""); //服药天数
            allprescriptionDataBean.setmID(data.getList().get(0).getPerscriptDetailList().get(i).getMid() + "");
            allprescriptionDataBean.setId(data.getList().get(0).getPerscriptDetailList().get(i).getId() + "");
            allprescriptionDataBean.setPlId(data.getList().get(0).getPerscriptDetailList().get(i).getPlId() + "");
            List<AllprescriptionDataBean.PrescriptionDoseDetailBean> mlist = new ArrayList<>();
            for (int a = 0; a < data.getList().get(0).getPerscriptDetailList().get(i).getPrescriptionDoseDetail().size(); a++) {
                AllprescriptionDataBean.PrescriptionDoseDetailBean prescriptionDoseDetailBean = new AllprescriptionDataBean.PrescriptionDoseDetailBean();
                prescriptionDoseDetailBean.setDetailId(data.getList().get(0).getPerscriptDetailList().get(i).getPrescriptionDoseDetail().get(a).getDetailId());
                prescriptionDoseDetailBean.setId(data.getList().get(0).getPerscriptDetailList().get(i).getPrescriptionDoseDetail().get(a).getId());
                prescriptionDoseDetailBean.setPlId(data.getList().get(0).getPerscriptDetailList().get(i).getPrescriptionDoseDetail().get(a).getPlId());
                prescriptionDoseDetailBean.setUseCount(data.getList().get(0).getPerscriptDetailList().get(i).getPrescriptionDoseDetail().get(a).getUseCount()+"");
                prescriptionDoseDetailBean.setUseTime(data.getList().get(0).getPerscriptDetailList().get(i).getPrescriptionDoseDetail().get(a).getUseTime());
                mlist.add(prescriptionDoseDetailBean);
            }
            allprescriptionDataBean.setPrescriptionDoseDetail(mlist);
            beanList.add(allprescriptionDataBean);
        }
    }



    @Override
    public void onError(String msg) {

        if (msg != null) {
            msg(msg);
        }
        pd.dismiss();
    }

    @Override
    public void onRefreshUI() {
        adapter.notifyDataSetChanged();
        pd.dismiss();
    }

    @Override
    public void onSeccessHistory(PrescriptionDayData data) {
        pd.dismiss();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    //刷新界面
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(UpdateEvent updateEvent) {
        if (updateEvent.type.equals(ConstantManger.MYGUESS)) {
            initData();
            ViseLog.d("刷新home药单界面");
        }
    }
}
